package com.xiaoyun.rollutil.util;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.xiaoyun.rollutil.config.RollProperties;
import com.xiaoyun.rollutil.constant.RollConstants;
import com.xiaoyun.rollutil.entity.bo.ProvinceBO;
import com.xiaoyun.rollutil.entity.bo.ApiModelBO;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/**
 * @Author: XiaoYun461
 * @Date: 2021/12/2 18:39
 */
@Slf4j
public class RequestUtil {


    /**
     * 远程请求  BAR_CODE_URL
     *
     * @param barcode 条形码编码
     * @return
     */
    public static final ApiModelBO getBarCodeModel(Long barcode) {
        return getApiModel(String.format(RollConstants.BAR_CODE_URL,barcode));
    }

    /**
     * 远程请求  TODAY_WEATHER_URL
     *
     * @param cityName 城市名字
     * @return
     */
    public static final ApiModelBO getTodayWeatherModel(String cityName) {
        return getApiModel(String.format(RollConstants.TODAY_WEATHER_URL, cityName));
    }

    /**
     * 远程请求  RUBBISH_URL
     *
     * @param name 垃圾
     * @return
     */
    public static final ApiModelBO getRubbishModel(String name) {
        return getApiModel(String.format(RollConstants.RUBBISH_URL, name));
    }

    /**
     * 远程请求  CITY_LIST_URL
     *
     * @return
     */
    public static final ApiModelBO getCityListModel() {
        return getApiModel(RollConstants.CITY_LIST_URL);
    }

    private static ApiModelBO getApiModel(String url) {
        String jsonBody = HttpUtil.createGet(url)
                .header(RollConstants.ROLL_APP_ID, RollProperties.appId)
                .header(RollConstants.ROLL_APP_SECRET, RollProperties.appSecret)
                .execute().body();
        log.info("jsonBody:{}", jsonBody);
        ApiModelBO apiModelBO = JSONUtil.toBean(jsonBody, ApiModelBO.class);
        return apiModelBO;
    }



}
